#!/usr/bin/runhaskell

{-# OPTIONS -XTypeOperators #-}

import System( getArgs )
import System.Console.GetOpt
import Data.Maybe( fromMaybe )
import Data.Binary

import FTable( (:->:), toList )

data Flag = Version | Input String | Output String

main = do
   args <- getArgs
   case getOpt RequireOrder options args of
      (flags, [],      [])     -> mapM_ action flags 
      (_,     nonOpts, [])     -> error $ "unrecognized arguments: " ++ unwords nonOpts
      (_,     _,       msgs)   -> error $ concat msgs ++ usageInfo header options
   
options :: [OptDescr Flag]
options = [ 
     Option ['i'] ["input"]   (ReqArg Input "FILE") "input a file"
   ]

header = "Usage: decode [OPTION...]"

action :: Flag -> IO ()
action (Input fn) =
    do
      g <- decodeFile fn :: IO (Int :->: Double)
      mapM_ print' $ toList g
      where
         print' (i,e) = putStrLn $ (show i) ++" "++ (show e)
  

